package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Template;
import cn.kgc.vue.service.TemplateService;
import cn.kgc.vue.mapper.TemplateMapper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;

/**
* @author mengshun
* @description 针对表【sys_template】的数据库操作Service实现
* @createDate 2024-10-08 08:44:27
*/
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>implements TemplateService{


    @Override
    public ResponseResult templatePage(QueryCondition<Template> queryCondition) {

        Page<Template> templatePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        String fileName = queryCondition.getSearchParams().getFileName();

        LambdaQueryWrapper<Template> lambda = new QueryWrapper<Template>().lambda();
        lambda.like(StringUtils.isNotBlank(fileName),Template::getFileName,fileName)
                .orderByDesc(Template::getCreateTime);

        baseMapper.selectPage(templatePage,lambda);


        return ResponseResult.success()
                .data("total",templatePage.getTotal())
                .data("tableData",templatePage.getRecords());
    }

    @Override
    public ResponseResult deleteTemplate(String uuidName) {

        // 删除资源信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("uuid_name",uuidName);
        baseMapper.deleteByMap(map);

        // 删除资源文件
        File file = new File("D:\\upload\\springboot\\" + uuidName);

        if (file.exists()){
            file.delete();
        }
        return ResponseResult.success();
    }
}




